package com.ywjt.platformbusiness.store.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


import com.ywjt.platformbusiness.R;
import com.ywjt.platformbusiness.base.BaseActivity;
import com.ywjt.platformbusiness.common.UserData;
import com.ywjt.platformbusiness.constants.UrlConstants;
import com.ywjt.platformbusiness.http.HttpRequest;
import com.ywjt.platformbusiness.http.HttpStringCallBack;
import com.ywjt.platformbusiness.store.adapter.IncomeDetailAdapter;
import com.ywjt.platformbusiness.store.model.ByYearModel;
import com.ywjt.platformbusiness.store.model.IncomeListModel;
import com.ywjt.platformbusiness.util.JsonUtils;
import com.ywjt.platformbusiness.widget.IncomMonthSelectBottomDialog;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class IncomeDetailActivity  extends BaseActivity implements View.OnClickListener {

    private RecyclerView rcCommon;
    private IncomeDetailAdapter adapter;
    private LinearLayout llDetailInCome;
    private TextView tvTime;

    private String starTime="";

    public static void actionStart(Context context) {
        Intent intent = new Intent(context, IncomeDetailActivity.class);
        context.startActivity(intent);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.llDetailInCome:
                IncomMonthSelectBottomDialog dialog=new IncomMonthSelectBottomDialog(this);
                dialog.setClicklistener(new IncomMonthSelectBottomDialog.ClickListenerInterface() {
                    @Override
                    public void confirm(String year) {
                        tvTime.setText(year);
                        starTime=year;
                        dialog.dismiss();
                        getIncome();
                    }
                });
                dialog.show(getSupportFragmentManager());
                break;
        }
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        setAndroidNativeLightStatusBar(true);
    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.incom_detail_activity;
    }

    @Override
    protected void initView() {
        setTitleText("明细");
        llDetailInCome=findViewById(R.id.llDetailInCome);
        rcCommon=findViewById(R.id.rcCommon);
        tvTime=findViewById(R.id.tvTime);



        llDetailInCome.setOnClickListener(this::onClick);
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM");
        starTime=simpleDateFormat.format(System.currentTimeMillis());
        tvTime.setText(starTime);
        getIncome();
    }


    @Override
    protected void initData() {

    }

    /**
     * 获取要使用的标题栏样式(此函数由子类重写)
     *
     * @return
     */
    @Override
    protected int getTitleBarType() {
        return 0;
    }

    //按月
    private void getIncome() {
        JSONObject jsonObject=new JSONObject();
        try {
            jsonObject.put("starTime",starTime);
            jsonObject.put("shopId",
                    UserData.create(IncomeDetailActivity.this).get(UserData.MERCHANTID));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        new HttpRequest(getContext()).doPost(UrlConstants.merchant_getIncomeDetail, "", jsonObject,new HttpStringCallBack() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onSuccess(Object result) {
                try {
                    String data = result.toString();
                    //把请求得数据转换成json对象
                    JSONObject jsonObject = null;
                    jsonObject = new JSONObject(data);
                    String message = jsonObject.getString("message");
                    Boolean success=jsonObject.getBoolean("success");
                    if(success){
                        Object obj = JsonUtils.fromJson(result.toString(), IncomeListModel.class);
                        if (obj instanceof IncomeListModel) {
                            IncomeListModel model=(IncomeListModel)obj;
                                rcCommon.setLayoutManager(new LinearLayoutManager(IncomeDetailActivity.this));
                                adapter=new IncomeDetailAdapter(model.getResult());
                                rcCommon.setAdapter(adapter);
                        }
                    }else {
                        Toast.makeText(getContext(),message,Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int code, String msg) {
                Toast.makeText(getContext(),msg,Toast.LENGTH_SHORT).show();
            }
        });
    }
}